//GM_observer.del('response','http://jf.10086.cn/rush/ngves.asiainfo.portal.rushbuy.web.RushBuyAction?action=rushBuyWare');
//GM_observer.add('response','http://jf.10086.cn/rush/ngves.asiainfo.portal.rushbuy.web.RushBuyAction?action=rushBuyWare','get','wgu','Location');
//GM_observer.add('response','http://jf.10086.cn/rush/ngves.asiainfo.portal.rushbuy.web.RushBuyAction?action=rushBuyWare','set','Location',null);
var config={
'11':{
	'11111111111110001111111111111000111111111111100011000011000000001100001100000000110001111000000011111111111000000111110011110000011110000111100000000000000110000000000000001000':'R'
   ,'00001111100000000011111111100000011111111111000001110000011100001110000000111000110000000001100011000000000110001100000000011000110000011111100011000001111110000110000111110000':'G'
   ,'00001111100000000011111111100000011111111111000001110000011100001110000000111000110000000001100011000000000110001100000000011000110000011111100011000001111110000110000111111000':'G'
   ,'11000000000000001100000000000000110000000000000011000000000000001111111111111000111111111111100011111111111110001100000000000000110000000000000011000000000000001100000000000000':'T'
   ,'00001111100000000011111111100000011111111111000001110000011100001110000000111000110000000001100011000000000110001100000000011000110000000001100011000000000110000110000000110000':'C'
   ,'00000001110000000000001111000000000001101100000000011100110000000011100011000000011000001100000011111111111110001111111111111000111111111111100000000000110000000000000011000000':'4'
   ,'11111111111110001111111111111000111111111111100000000110000000000000011000000000000001100000000000000110000000000000011000000000111111111111100011111111111110001111111111111000':'H'
   ,'11111111111110001111111111111000111111111111100011000000000110001100000000011000110000000001100011000000001110001111000001111000011111111111000000111111111000000000111110000000':'D'
   ,'11111111111110001111111111111000111111111111100011000000000110001100000000011000110000000001100011100000001110001111000001111000011111111111000000111111111000000000111110000000':'D'
   ,'11111111111110001111111111111000011110000000000000111100000000000001111000000000000001110000000000000011110000000000000111100000000000001111000011111111111110001111111111111000':'N'
   ,'10000000000000001110000000000000111110000000000001111100000000000001111111111000000001111111100000000111111110000001110000000000011110000000000011100000000000001000000000000000':'Y'
  }
,'9':{
	'111111111111100011111111111110001111111111111000110001100001100011000110000110001111111100011000111110111111100001110001111110000000000011110000':'B'
   ,'110000000000000011000000000110001100000001111000110000011111100011000011110000001100111100000000110111000000000011110000000000001110000000000000':'7'
   ,'111111111111100011111111111110001111111111111000000000000001100000000000000110000000000000011000000000000001100000000000000110000000000000011000':'L'
   ,'111111111111100011111111111110001111111111111000110001100001100011000110000110001100011000011000110001100001100011000110000110001100000000011000':'E'
   ,'111111111111100011111111111110001111111111111000110001100000000011000110000000001100111000000000111111100000000011111100000000000111100000000000':'P'
   ,'011000000011100011000000011110001100000011111000110000001101100011000001100110001110001100011000111111100001100001111100000110000011100000011000':'2'
}
,'7':{
	'0000000000000011000000000000001100000000000000110000000000000011111111111111111111111111111111101111111111111100':'J'
}
,'10':{
	'0011110000000000011111100011000011111111000110001110011100011000110000110001100011000011001110001110011001111000011111111111000000111111111000000001111110000000':'9'
   ,'0000111111000000001111111110000001111111111100001111001100111000111001100001100011000110000110001100011100111000110001111111100001100011111100000000000111100000':'6'
   ,'1100000000111000110000000111100011000000111110001100000111011000110001111001100011001111000110001101110000011000111110000001100011110000000110001110000000011000':'Z'
   ,'1111111111100000111111111111000011111111111110000000000000111000000000000001100000000000000110000000000000011000000000000011100011111111111100001111111111100000':'U'
   ,'0000000011100000001110011111000001111111111110001111111100111000110011100001100011000111000110001111111110011000011111111111100000111001111100000000000011100000':'8'
}
,'13':{
	'1111111111111000111111111111100011111000000000001111111100000000001111111111000000000111111100000000000001110000000001111111000000111111100000001111100000000000111111111111100011111111111110001111111111111000':'M'
}
,'12':{
	'100000000000100011000000000110001111000000110000011110001110000000111111110000000000111100000000000001111000000000011111111000000011100011110000011000000111100011000000000110001000000000001000':'X'
   ,'111111111111100011111111111110001111111111111000000000100000000000000111000000000000111110000000000110011100000000110000111000000110000001110000110000000011100010000000000110000000000000001000':'K'
   ,'000000000000100000000000011110000000001111110000000111111100000011111100110000001110000011000000111111001100000011111111110000000001111111100000000000111111100000000000011110000000000000001000':'A'
   ,'100000000000000011110000000000001111111000000000011111111100000000001111111110000000000111111000000000000011100000000001111110000000111111000000011111100000000011110000000000001000000000000000':'V'
}
,'15':{
	'111000000000000011111111100000001111111111111000000111111111100000000000001110000000011111111000111111111000000011110000000000001111111110000000111111111111100000000111111110000000000001111000000111111111100011111111100000001110000000000000':'W'
}
,'8':{
	'11111111111110001111111111111000111111111111100011000110000000001100011000000000110001100000000011000110000000001100000000000000':'F'
   ,'00111000001100000111110000011000111111100001100011000110000110001100011100011000110000111111100001100011111100000000000011100000':'S'
   ,'00111000001100000111110000011000111111100001100011000110000110001100011100011000110000111111100001100011111100000000000111100000':'S'
   ,'11111110001100001111111000011000110001100001100011000110000110001100011100111000110000111111100011000011111100001100000111100000':'5'
   ,'01100000001100001100011000011000110001100001100011000110000110001100111100111000111111111111100001111001111100000011100011100000':'3'
   ,'01100000001100001100011000011000110001100001100011000110000110001100111100111000111111111111100001111001111100000011000011100000':'3'
}
,'14':{
	'00001111100000000011111111100000011111111111000011110000011110001110000000111000110000000001100011000000000110001110000000111100111100000111110001111111111101100011111111100110000011111000011100000000000000110000000000000010':'Q'
}
};

var document=unsafeWindow.document,img=document.getElementById("valid_image"),$=unsafeWindow.$;
unsafeWindow.wgu=0;
document.getElementById('rush_form').target='_blank';
$('.popInfoDetail').append('<canvas id="c" ></canvas>');
img.onload=function (){
		try{
			new yzm(this,document.getElementById("c"))
				.removeBG()
				.dealLine();
		}catch (e) {
			unsafeWindow.imageAgain();
		}
};

	function getRelation(g,l){
		var i=0,j=0,a=g[l-1],b=g[l],la=a.length,lb=b.length;
		for(i=0;i<la;i++){
			if(a[i]==b[i]&&a[i]==1){
				return 1;
			}
		}
		return 0;
	}

	function yzm(s,r){
		this.src=s.src.substr(s.src.length-10).toUpperCase();
		this.w=s.width;
		this.h=s.height;
		this.d={};
		this.container=r.getContext("2d");
		this.container.drawImage(s,0, 0,this.w,this.h);
		this.frame = getImgData(this.container,this.w,this.h);
		this.data = this.frame.data;
		this.b=[];
		this.line=[];
		this.g=[];
	}
	yzm.prototype={
		getRGB : function (i,j){
			var s = ( i * this.w + j ) * 4 ;
			return ( this.data[0 + s] << 16 ) + ( this.data[1 + s] << 8 ) + this.data[2 + s];
		},
		removeBG : function (){
			var h=this.h,w=this.w,i=0,j=0,c=0,g=this.g,f=-1,line=this.line;
			for (j=0;j<(w-2);j++){
				f=-1;
				g[j]=[];
				line[j]=[];
				for (i=0;i<(h-4);i++){
					c = this.getRGB(i+3,j+1) ;
					if(c>10000000){
						g[j][i]=0;
						if(f==1){
							line[j].push(i-1);
							f=-1;
						}
					}else{
						g[j][i]=1;
						if(f==-1){
							line[j].push(i);
							f=1;
						}
					}
					
				}
				if(f==1){
					line[j].push(i-1);
				}
			}
			return this;
		},
		dealLine : function(){
			var i=0,line=this.line,l=line.length,f=-1,a=[],g=this.g,t='',valid='',r='';
			for(i=0;i<l;i++){
				if(line[i].length==0){
					if(f==-1){
						continue;
					}
					if(f==1){
						a.push(i-1);
						f=-1;
					}
				}else{
					if(f==-1){
						f=1;
						a.push(i);
					}else{
						if(getRelation(g,i)==0){
							a.push(i-1);
							a.push(i);
						}
					}
				}
			}
			if(a.length!=12){
				unsafeWindow.imageAgain();
			}else{
				for(i=0;i<a.length;i+=2){
					t='';
					for(j=a[i];j<=a[i+1];j++){
						t+=g[j].join('');
					}
					r=rectify(config[a[i+1]-a[i]+1],t);
					if(r=='undefined'){
						unsafeWindow.imageAgain();
					}else{
						valid+=r;
					}
				}
			}
			document.getElementById("valid_code_tmp").value=valid;
			document.getElementById('rushbuy_confirm').disabled=false;
			$("#valid_code").val($("#valid_code_tmp").val());
			//if(unsafeWindow.wgu==1){
			//	unsafeWindow.wgu_post();
			//}
			$("#rush_form").submit();
		}
	}

	function rectify(o,s){
		var t='',i=0,l=s.length,r=0;
		if(o[s]){
			return o[s];
		}
		for( t in o){
			r=0;
			for(i=0;i<l;i++){
					if(t[i]!=s[i]){
						r++;
					}
			}
			if(r<2){
				return o[t];
			}
		}
		return 'undefined';
	}
	
	
	unsafeWindow.wgu_post=function (i){
	    var warIds=[100000001564204,100000001644210,100000001644208,100000001644204];
		return function (){
			unsafeWindow.wgu=1;
			try{
				var req = new XMLHttpRequest(); 
				req.open('POST', 'http://jf.10086.cn/rush/ngves.asiainfo.portal.rushbuy.web.RushBuyAction?action=rushBuyWare', false);  
				req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');		 			
				req.send('warId='+warIds[i]+'&mobileNumber=15850116859&memcacheKey='+document.getElementById('memcacheKey').value+'&activityId='+document.getElementById('activityId').value+'&valid_code='+$("#valid_code_tmp").val());
				if(req.getResponseHeader('wgu').indexOf('errorMessage')!=-1){
					if(req.getResponseHeader('wgu').indexOf('ACTIVITY_LIMIT_INVALID')!=-1){
						unsafeWindow.imageAgain();
					}else if(req.getResponseHeader('wgu').indexOf('VALID_CODE_ERROR')!=-1){
						unsafeWindow.imageAgain();
					}else if(req.getResponseHeader('wgu').indexOf('GRANT_MAX_AMOUNT_IS_ZERO')!=-1){
						i++;
						if(i<warIds.length){
							unsafeWindow.imageAgain();
						}
					}else{
						console.log(req.getResponseHeader('wgu'));
						window.open(req.getResponseHeader('wgu'));	
					}
				}else{
					console.log(req.getResponseHeader('wgu'));
					window.open(req.getResponseHeader('wgu'));
				}
			}catch (e) {
				unsafeWindow.imageAgain();
			}
		}
	}(0);
